//
//  XrDataBaseManager.m
//  TasteEnjoy
//
//  Created by XuRui on 16/5/5.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#import "XrDataBaseManager.h"

@interface XrDataBaseManager ()

//数据库对象(作用类似:sqlite中dbPoint)
@property (nonatomic, retain) FMDatabase *fmdb;

@end


@implementation XrDataBaseManager

- (void)dealloc
{
    [_fmdb release];
    [super dealloc];
}

#pragma mark - ------------ 单例 ---------------

+ (XrDataBaseManager *)shareInstace
{
    static XrDataBaseManager *DBManager = nil; /**< 全局只有一个 */

    if (!DBManager) {
        DBManager = [[XrDataBaseManager alloc]init];
    }
    return DBManager;
    
}

#pragma mark - ------------ 打开数据库 ---------------

- (void)openDataBase
{

    if (_fmdb != nil) {
        
        NSLog(@"数据库 已经打开");
        return;
    }
    //获取数据库路径
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"fmdb.db"];
    NSLog(@"收藏数据库路径 %@",path);
    
    //创建数据库对象
    self.fmdb = [[FMDatabase alloc]initWithPath:path];
    
    BOOL result = [_fmdb open];
    //判断
    if (result) {
        NSLog(@"数据库打开成功");
    }else{
    
        NSLog(@"数据库打开失败");
    }

}

#pragma mark - ------------ 创建表 删除表 ---------------

- (void)createTable
{

    NSString *creatSQL = @"create table if not exists enjoyApp (number integer PRIMARY KEY Autoincrement, title text, productID text)";
    
    [self executeSQL:creatSQL message:@"创建表"];

}


- (void)deleteTable
{

    NSString *deleteSQL = @"drop table enjoyApp";
    [self executeSQL:deleteSQL message:@"删除表"];
}


#pragma mark - ------------ 增 ---------------

- (void)insertEnjoy:(EnjoyModel *)enjoy
{


    NSString *insertSQL = [NSString stringWithFormat:@"insert into enjoyApp(title, productID) values ('%@', '%@')", enjoy.short_name, enjoy.product_id];
    [self executeSQL:insertSQL message:@"插入数据"];

}

#pragma mark - ------------ 删 ---------------
- (void)deleteTitle:(NSString *)title
{

    NSString *deleteSQL = [NSString stringWithFormat:@"delete from enjoyApp where title = '%@'", title];
    [self executeSQL:deleteSQL message:@"删除数据"];
}

#pragma mark - ------------ 改 ---------------
- (void)updateEnjoy:(EnjoyModel *)newValue num:(NSInteger)num
{

    NSString *updateSQL = [NSString stringWithFormat:@"update enjoyApp set title = '%@', productID = '%@' where number = %ld", newValue.short_name, newValue.product_id, num];
    
    [self executeSQL:updateSQL message:@"修改数据"];
}

#pragma mark - ------------ 查 ---------------
- (NSMutableArray *)select
{

    NSMutableArray *mArr = [NSMutableArray array];
    //sql
    NSString *selectSQL = @"select * from enjoyApp";
    //执行查询 遍历每一行信息
    FMResultSet *resultSet = [_fmdb executeQuery:selectSQL];
    if(resultSet){
        
    
    while ([resultSet next]) {
        
        NSString *title = [resultSet stringForColumn:@"title"];
        NSString *productID = [resultSet stringForColumn:@"productID"];
        //创建对象
        EnjoyModel *m = [[EnjoyModel alloc]init];
        m.short_name = title;
        m.product_id = (NSNumber *)productID;
        
        [mArr addObject:m];
        
    }
        return mArr;
    }else{
    
        NSLog(@"查询未果");
        return nil;
    }
}




- (void)executeSQL:(NSString *)sql message:(NSString *)mes
{

    //执行
    BOOL result = [_fmdb executeUpdate:sql];
    if (result) {
        NSLog(@"%@成功", mes);
    }else{
        NSLog(@"%@失败", mes);
    }
    
    
}


@end
